
package com.wyzz.hr.base;

import java.util.*;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.SysConfigManager;

public class ComonBeans {

	public ComonBeans() {
	}

	public static Map getAllProperties() {
		return allProperties;
	}

	public static void reInit() {
		allProperties.clear();
		PropertiesConfiguration config = null;
		try {
			config = new PropertiesConfiguration("comombeans.properties");
			config.setEncoding("utf-8");
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		String key;
		for (Iterator it = config.getKeys(); it.hasNext(); allProperties.put(
				key, (String) config.getProperty(key)))
			key = (String) it.next();

		allPropertiesInGroup.clear();
		resultEmpPolitics.clear();
		resultEmpDiploma.clear();
		resultRecaEmpContact.clear();
		resultRecaHRContact.clear();
		resultEmpSpeciality.clear();
		resultEmpCityNo.clear();
		resultEmpNation.clear();
		empSalaryRange.clear();
	}

	public static Map getAllPropertiesFromResourceBundle() {
		Map allProperties = new HashMap();
		String keyTmp;
		for (Enumeration keys = COMMON_BEAN_RESOURCE_BUNDLE.getKeys(); keys
				.hasMoreElements(); allProperties.put(keyTmp,
				COMMON_BEAN_RESOURCE_BUNDLE.getString(keyTmp))) {
			keyTmp = (String) keys.nextElement();
			COMMON_BEAN_RESOURCE_BUNDLE.getString(keyTmp);
		}

		return allProperties;
	}

	public static Map getValuesToMap(String key, boolean addNullOption[]) {
		Map resultTmp;
		if (allPropertiesInGroup.containsKey(key)) {
			resultTmp = (Map) allPropertiesInGroup.get(key);
		} else {
			int len = key.length() + 1;
			resultTmp = new TreeMap();
			Iterator it = allProperties.keySet().iterator();
			do {
				if (!it.hasNext())
					break;
				String keyTmp = (String) it.next();
				if (keyTmp.indexOf((new StringBuilder()).append(key)
						.append("_").toString()) == 0)
					resultTmp.put(keyTmp.substring(len), allProperties
							.get(keyTmp));
			} while (true);
			if (resultTmp.size() > 0)
				allPropertiesInGroup.put(key, resultTmp);
		}
		Map result = new TreeMap();
		String keyTmp;
		for (Iterator it = resultTmp.keySet().iterator(); it.hasNext(); result
				.put(keyTmp, resultTmp.get(keyTmp)))
			keyTmp = (String) it.next();
		if (addNullOption.length > 0 && addNullOption[0])
			result.put("", allProperties.get("selectNullOption"));
		return result;
	}

	public static Map getValuesToMap(String key, String index[],
			boolean addNullOption[]) {
		Map keyMap = getValuesToMap(key, new boolean[0]);
		Map result = new TreeMap();
		for (int i = 0; i < index.length; i++)
			result.put(index[i], keyMap.get(index[i]));

		if (addNullOption.length > 0 && addNullOption[0])
			result.put("", allProperties.get("selectNullOption"));
		return result;
	}

	public static Map getValuesToMap(String key, Integer index[],
			boolean addNullOption[]) {
		String indexStr[] = new String[index.length];
		for (int i = 0; i < index.length; i++)
			indexStr[i] = (new StringBuilder()).append(index[i]).append("")
					.toString();

		return getValuesToMap(key, indexStr, addNullOption);
	}

	public static Map getValuesToMapInverse(String key) {
		Map keyMap = getValuesToMap(key, new boolean[0]);
		Map result = new TreeMap();
		String keyTmp;
		for (Iterator it = keyMap.keySet().iterator(); it.hasNext(); result
				.put(keyMap.get(keyTmp), keyTmp))
			keyTmp = (String) it.next();

		return result;
	}

	public static String getParameterValue(String key, String index[]) {
		String keyInMap = key;
		for (int i = 0; i < index.length; i++)
			keyInMap = (new StringBuilder()).append(keyInMap).append("_")
					.append(index[i]).toString();

		return (String) allProperties.get(keyInMap);
	}

	public static Map getSecondLevelSelect(Map topMap, String keySub,
			boolean addNullOption[]) {
		Map result = new TreeMap();
		Map subMap = getValuesToMap(keySub, new boolean[0]);
		String defaultKey = "";
		Iterator itr = topMap.keySet().iterator();
		do {
			if (!itr.hasNext())
				break;
			String keyTmp = (String) itr.next();
			result.put(keyTmp, new TreeMap());
			if (keyTmp.compareTo(defaultKey) > 0)
				defaultKey = keyTmp;
		} while (true);
		int defaultKeyLength = defaultKey.length();
		String keyTmp;
		String keyTop;
		String valueTmp;
		for (Iterator it = subMap.keySet().iterator(); it.hasNext(); ((Map) result
				.get(keyTop)).put(keyTmp, valueTmp)) {
			keyTmp = (String) it.next();
			keyTop = keyTmp.substring(0, defaultKeyLength);
			valueTmp = (String) subMap.get(keyTmp);
		}

		Map defaultMap = (Map) result.get(defaultKey);
		Iterator it = result.keySet().iterator();
		do {
			if (!it.hasNext())
				break;
			String keyTop1 = (String) it.next();
			if (keyTop1.length() > 0 && ((Map) result.get(keyTop1)).size() == 0)
				result.put(keyTop1, defaultMap);
		} while (true);
		if (addNullOption.length > 0 && addNullOption[0]) {
			String nullValue = (String) allProperties.get("selectNullOption");
			Iterator itrr = result.values().iterator();
			do {
				if (!itrr.hasNext())
					break;
				Map mapTmp = (Map) itrr.next();
				if (defaultMap != mapTmp)
					mapTmp.put("", nullValue);
			} while (true);
			defaultMap.put("", nullValue);
		}
		return result;
	}

	public static List getEmpSalaryRange() {
		if (empSalaryRange.isEmpty()) {
			int index = 1;
			int temp = index;
			String currentContext = null;
			String currentContext2 = null;
			while ((currentContext = getParameterValue((new StringBuilder())
					.append("EmpSalaryRange_").append(index).toString()))
					.length() > 0) {
				if (index == 1) {
					empSalaryRange.add((new StringBuilder()).append("<")
							.append(currentContext).toString());
				} else {
					temp = index - 1;
					currentContext2 = getParameterValue((new StringBuilder())
							.append("EmpSalaryRange_").append(temp).toString());
					empSalaryRange.add((new StringBuilder()).append(
							currentContext2).append("-").append(currentContext)
							.toString());
				}
				index++;
			}
			empSalaryRange.add((new StringBuilder()).append(">").append(
					getParameterValue((new StringBuilder()).append(
							"EmpSalaryRange_").append(index - 1).toString()))
					.toString());
		}
		return empSalaryRange;
	}

	public static List getEmpPolitics() {
		if (resultEmpPolitics.isEmpty()) {
			int index = 1;
			for (String currentContext = null; (currentContext = getParameterValue((new StringBuilder())
					.append("EmpPolitic_").append(index).toString())).length() > 0;) {
				resultEmpPolitics.add(currentContext);
				index++;
			}

		}
		return resultEmpPolitics;
	}

	public static List getEmpDiploma() {
		if (resultEmpDiploma.isEmpty()) {
			int index = 1;
			for (String currentContext = null; (currentContext = getParameterValue((new StringBuilder())
					.append("EmpDiploma_").append(index).toString())).length() > 0;) {
				resultEmpDiploma.add(currentContext);
				index++;
			}

		}
		return resultEmpDiploma;
	}

	public static List getRecaHRContact() {
		if (resultRecaHRContact.isEmpty()) {
			int index = 0;
			for (String currentContext = null; (currentContext = getParameterValue((new StringBuilder())
					.append("recaHRContact_").append(index).toString()))
					.length() > 0;) {
				resultRecaHRContact.add(currentContext);
				index++;
			}

		}
		return resultRecaHRContact;
	}

	public static List getRecaEmpContact() {
		if (resultRecaEmpContact.isEmpty()) {
			int index = 0;
			for (String currentContext = null; (currentContext = getParameterValue((new StringBuilder())
					.append("recaEmpContact_").append(index).toString()))
					.length() > 0;) {
				resultRecaEmpContact.add(currentContext);
				index++;
			}

		}
		return resultRecaEmpContact;
	}

	public static List getEmpSpeciality() {
		if (resultEmpSpeciality.isEmpty()) {
			int index = 1;
			for (String currentContext = null; (currentContext = getParameterValue((new StringBuilder())
					.append("EmpSpeciality_").append(index).toString()))
					.length() > 0;) {
				resultEmpSpeciality.add(currentContext);
				index++;
			}

		}
		return resultEmpSpeciality;
	}

	public static List getEmpCityNo() {
		if (resultEmpCityNo.isEmpty()) {
			int index = 1;
			for (String currentContext = null; (currentContext = getParameterValue((new StringBuilder())
					.append("EmpCityNo_").append(index).toString())).length() > 0;) {
				resultEmpCityNo.add(currentContext);
				index++;
			}

		}
		return resultEmpCityNo;
	}

	public static List getEmpNation() {
		if (resultEmpNation.isEmpty()) {
			int index = 1;
			for (String currentContext = null; (currentContext = getParameterValue((new StringBuilder())
					.append("EmpNation_").append(index).toString())).length() > 0;) {
				resultEmpNation.add(currentContext);
				index++;
			}
		}
		return resultEmpNation;
	}

	public static String getParameterValue(String key) {
		String value = (String) allProperties.get(key);
		return value != null ? value.trim() : "";
	}

	public static String getShiftEnable() {
//		SysConfigManager dbConfigManager = DatabaseSysConfigManager
//				.getInstance();
//		String mode = dbConfigManager.getProperty("sys.examin.shift.enable");
//		return mode;
		return "1";
	}

	public static final String COMMON_BEAN_RESOURCE_FILE_NAME = "comombeans";
	private static final String _SEPERATOR = "_";
	public static ResourceBundle COMMON_BEAN_RESOURCE_BUNDLE = PropertyResourceBundle
			.getBundle("comombeans");
	private static Map allProperties = getAllPropertiesFromResourceBundle();
	private static Map allPropertiesInGroup = new HashMap();
	public static final String _YESORNO = "yesOrNo";
	public static final String _NOORYES = "noOrYes";
	public static final String _YESORNOIMG = "yesOrNoImg";
	public static final String _ORDER_ASCORDESC = "orderAscOrDesc";
	public static final String _SELECT_NULL_OPTION = "selectNullOption";
	private static final String EmpPoliticsStarts = "EmpPolitic";
	private static final String EmpDiplomaStarts = "EmpDiploma";
	private static final String RecaEmpContactStarts = "recaEmpContact";
	private static final String RecaHRContact = "recaHRContact";
	private static final String EmpSpecialityStarts = "EmpSpeciality";
	private static final String EmpCityNoStarts = "EmpCityNo";
	private static final String EmpNationStarts = "EmpNation";
	private static final String EmpSalaryRangeStarts = "EmpSalaryRange";
	private static List resultEmpPolitics = new ArrayList();
	private static List resultEmpDiploma = new ArrayList();
	private static List resultRecaEmpContact = new ArrayList();
	private static List resultRecaHRContact = new ArrayList();
	private static List resultEmpSpeciality = new ArrayList();
	private static List resultEmpCityNo = new ArrayList();
	private static List resultEmpNation = new ArrayList();
	private static List empSalaryRange = new ArrayList();

}
